{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ac0a103",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install gym[atari]\n",
    "!pip install gym[accept-rom-license]\n",
    "!pip install ray==1.2 matplotlib\n",
    "!pip install dm-tree dataclasses ray[rllib]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d42fc77",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec1fe0b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import subprocess\n",
    "\n",
    "\n",
    "def system_cmd(cmd_list):\n",
    "    cmd = subprocess.run(cmd_list, shell=False)\n",
    "    if cmd.returncode != 0:\n",
    "        print('cannot exec cmd: {}, exit with {}'.format(cmd_list, cmd.returncode))\n",
    "        raise EnvironmentError\n",
    "    return\n",
    "\n",
    "\n",
    "def install_gym(mode=None):\n",
    "    if mode is not None:\n",
    "        p_path = os.environ.get('SITE_PACKAGES_PATH', '/home/ma-user/anaconda/lib/python3.6/site-packages')\n",
    "        file_path = os.path.join(p_path, 'AutoROM/roms')\n",
    "        target = os.path.join(p_path, 'atari_py/atari_roms')\n",
    "        system_cmd(['cp', '-rf', file_path, target])\n",
    "    return\n",
    "\n",
    "\n",
    "install_gym(mode='atari')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import json\n",
    "import pickle\n",
    "import logging\n",
    "import requests\n",
    "import gym\n",
    "from ray.rllib.env.wrappers.atari_wrappers import wrap_deepmind\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython import display\n",
    "\n",
    "logger = logging.getLogger(__name__)\n",
    "logging.basicConfig(level=logging.INFO,\n",
    "                    format=\"[%(asctime)s] [%(levelname)s] [%(message)s]\",\n",
    "                    datefmt='%Y-%m-%d %H:%M:%S'\n",
    "                    )\n",
    "\n",
    "APP_URL = ''        # 部署服务的访问地址\n",
    "D_NAME = ''              # 账号名\n",
    "IAM_NAME = ''    # IAM用户名\n",
    "PASSWORD = ''        # 密码\n",
    "\n",
    "\n",
    "def create_env(name='PongNoFrameskip-v4'):\n",
    "    env = gym.make(name)\n",
    "    env = wrap_deepmind(\n",
    "        env,\n",
    "        dim=42,\n",
    "        framestack=False,\n",
    "        framestack_via_traj_view_api=False)\n",
    "    return env\n",
    "\n",
    "\n",
    "def prepare_test_config(name=IAM_NAME, domain_name=D_NAME, passwd=PASSWORD):\n",
    "    config = {\"app_url\": APP_URL, \"header_content_type\": \"text/plain\", \"local\": False}\n",
    "    header = {\n",
    "        \"Content-Type\": \"application/json\"\n",
    "    }\n",
    "    body = {\n",
    "        \"auth\": {\n",
    "            \"identity\": {\n",
    "                \"methods\": [\"password\"],\n",
    "                \"password\": {\n",
    "                    \"user\": {\n",
    "                        \"name\": name,\n",
    "                        \"password\": passwd,\n",
    "                        \"domain\": {\n",
    "                            \"name\": domain_name\n",
    "                        }\n",
    "                    }\n",
    "                }\n",
    "            },\n",
    "            \"scope\": {\n",
    "                \"project\": {\n",
    "                    \"name\": \"cn-north-4\"\n",
    "                },\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    body = json.dumps(body)\n",
    "    response = requests.post(\"https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens?nocatalog=true\",\n",
    "                             headers=header, data=body, verify=False)\n",
    "    config[\"header_X_Auth_Token\"] = response.headers['X-Subject-Token']\n",
    "    print(\"config prepared\")\n",
    "    return config\n",
    "\n",
    "\n",
    "class PolicyClient(object):\n",
    "    \"\"\"\n",
    "    REST client to interact with a RLlib policy server.\n",
    "    input param:\n",
    "        configs (requires following dict):\n",
    "            app_url\n",
    "            local: True or False\n",
    "            header_X_Auth_Token\n",
    "            header_content_type\n",
    "\n",
    "    \"\"\"\n",
    "\n",
    "    def __init__(self, configs):\n",
    "        \"\"\"\n",
    "        configs must have following param:\n",
    "            app_url\n",
    "            valid_action\n",
    "        \"\"\"\n",
    "        self.configs = configs\n",
    "        self._address = self.configs[\"app_url\"]\n",
    "        self.env = create_env(name='PongNoFrameskip-v4')\n",
    "        self.data = None\n",
    "        self.parsed = None\n",
    "\n",
    "    def _send(self, data):\n",
    "        payload = pickle.dumps(data)\n",
    "        header = {\n",
    "            \"X-Auth-Token\": self.configs[\"header_X_Auth_Token\"],\n",
    "            \"Content-Type\": self.configs[\"header_content_type\"]\n",
    "        }\n",
    "        response = requests.post(self._address, headers=header, data=payload, verify=False)\n",
    "        if response.status_code != 200:\n",
    "            logger.error(\"Request failed {}: {}\".format(response.text, data))\n",
    "        response.raise_for_status()\n",
    "        self.parsed = pickle.loads(response.content)\n",
    "        return self.parsed\n",
    "\n",
    "    def launch(self):\n",
    "        obs = self.env.reset()\n",
    "        plt.imshow(obs)\n",
    "        rewards = 0\n",
    "\n",
    "        while True:\n",
    "            resp = self._send({\"obs\": list(obs)})\n",
    "            action = resp[\"action\"]\n",
    "            obs, reward, done, info = self.env.step(action)\n",
    "            plt.imshow(obs)\n",
    "            display.clear_output(wait=True)\n",
    "            plt.pause(0.0000001)\n",
    "            rewards += reward\n",
    "            # print(\"log returns: \", reward)\n",
    "            if done:\n",
    "                print('Total rewards:', rewards)\n",
    "                break\n",
    "        plt.show()\n",
    "\n",
    "\n",
    "config = prepare_test_config()\n",
    "client = PolicyClient(config)\n",
    "client.launch()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd0330cb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Conda-python3",
   "language": "python",
   "name": "conda-python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}